/**
 * 包含多个间接更新 state 或 调用 mutaions 的方法对象
 */
import {
  reqAddress,
  reqShopTypes,
  reqShops,
  reqUserInfo,
  reqLogout,
  reqFoods,
  reqRatings,
  reqInfo,
  reqKeyword
} from '../api/index'
import {
  RECEIVE_ADDRESS,
  RECEIVE_SHOPTYPES,
  RECEIVE_SHOPS,
  RECEIVE_USER_INFO,
  RECEIVE_LOGOUT,
  RECEIVE_INFO,
  RECEIVE_FOODS,
  RECEIVE_RATINGS,
  SET_FOOD_INCREMENT,
  SET_FOOD_DECREMENT,
  RESET_SHOP_CART,
  GET_RATING,
  RECEIVE_SEARCH_SHOP
} from './mutation_type'


export default {
  // 根据经纬度获取地址
  async getAddress({
    commit,
    state
  }) {
    // 1. 准备一个 geohash 对象
    const geohash = {
      latitude: state.latitude,
      longitude: state.longitude
    }
    // 2. 发送 请求，获取地址
    const result = await reqAddress(geohash);
    // 3. 判断响应的数据是否成功
    if (result.code === 0) {
      const address = result.data;
      // 4. 提交给 mutaions 处理
      commit(RECEIVE_ADDRESS, {
        address
      })
    }

  },

  // 获取商品分类
  async getShopTypes({
    commit
  }) {
    // 1. 发送 请求，获取商品分类
    const result = await reqShopTypes();
    // 2. 判断响应的数据是否成功
    if (result.code === 0) {
      const shopTypes = result.data;
      // 3. 提交给 mutaions 处理
      commit(RECEIVE_SHOPTYPES, {
        shopTypes
      })
    }

  },

  // 根据经纬度获取商铺列表
  async getShops({
    commit,
    state
  }) {
    const {
      latitude,
      longitude
    } = state;
    // 2. 发送 请求，获取地址
    const result = await reqShops(latitude, longitude);
    // 3. 判断响应的数据是否成功
    if (result.code === 0) {
      const shops = result.data;
      // 4. 提交给 mutaions 处理
      commit(RECEIVE_SHOPS, {
        shops
      })
    }

  },

  // 存储用户信息
  accessUserInfo({
    commit
  }, userInfo) {
    commit(RECEIVE_USER_INFO, {
      userInfo
    })
  },

  // 异步获取用户的信息
  async getUserInfo({
    commit
  }) {
    const result = await reqUserInfo();
    if (result.code == 0) {
      const userInfo = result.data;
      commit(RECEIVE_USER_INFO, {
        userInfo
      })
    }

  },

  // 异步退出
  async logout({
    commit
  }) {
    const result = await reqLogout();
    if (result.code === 0) {
      commit(RECEIVE_LOGOUT)
    }
  },

  // 异步获取商家信息
  async getGoodInfo({
    commit
  }) {
    const result = await reqInfo();
    if (result.code == 0) {
      const info = result.info;
      commit(RECEIVE_INFO, {
        info
      })
    }
  },
  // 异步获取评论信息
  async getRatings({
    commit
  }) {
    const result = await reqRatings();
    if (result.code == 0) {
      const ratings = result.ratings;
      commit(RECEIVE_RATINGS, {
        ratings
      })
    }
  },
  // 异步获取商品信息
  async getFoods({
    commit
  }, cb) {
    const result = await reqFoods();
    if (result.code == 0) {
      const foods = result.foods;
      commit(RECEIVE_FOODS, {
        foods
      })
      cb();
    }
  },
  // 商品规格的自增或自减
  setFoodCount({
    commit
  }, {
    flag,
    food
  }) {
    if (flag) {
      commit(SET_FOOD_INCREMENT, {
        food
      })
    } else {
      commit(SET_FOOD_DECREMENT, {
        food
      })
    }
  },
  // 清空购物车
  clearCart({
    commit
  }) {
    commit(RESET_SHOP_CART);
  },

  // 获取评论列表
  async getRatings({
    commit
  }, callback) {
    const result = await reqRatings();
    if (result.code === 0) {
      const ratings = result.ratings;
      commit(GET_RATING, {
        ratings
      })
      callback && callback()
    }
  },
  // 异步根据关键字搜索商家列表
  async getSearchShop({commit}, keyword) {
    const result = await reqKeyword({keyword});
    if (result.code == 0) {
      const searchShop = result.data;
      commit(RECEIVE_SEARCH_SHOP, {searchShop});
    }
  }
}
